package com.administration.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.administration.controller.AdministrationController;
import com.administration.entity.Access;
import com.administration.entity.Groups;
import com.administration.entity.GroupPrivilege;
import com.administration.entity.resultsetmapping.PrivilegeMapping;
import com.common.Constant;
import com.core.managedBean.BaseBean;

@ManagedBean
@ViewScoped
public class GroupPrivilegeBean extends BaseBean {
	private static final long serialVersionUID = 1L;

	private GroupPrivilege groupPrivilege;
	private Groups group;
	private PrivilegeMapping privilegeMapping;
	private List<GroupPrivilege> groupPrivilegeList;
	private List<Access> accessList;
	private List<PrivilegeMapping> privilegeMappingList;
	private List<Groups> groupList;
	// private administrationController administrationController = new
	// administrationControllerImpl();
	AdministrationController administrationController;;

	@PostConstruct
	public void loadData() {
		if (administrationController == null) {
			administrationController = new AdministrationController();
		}
		group = new Groups();
		loadAllList();
	}

	public void loadAllList() {
		try {
			this.accessList = administrationController.getAllAccess();
			this.privilegeMappingList = administrationController
					.getAllPrivilegeMappingList();
			this.groupList = administrationController.getAllGroups();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void saveGroupPrivilege() {
		try {
			if (groupPrivilege.getGroupId() == null) {
				Long groupId = administrationController
						.addGroupPrivileges(groupPrivilege);
				this.groupPrivilege.setGroupId(groupId);
			} else {
				administrationController.updateGroupPrivileges(groupPrivilege);
			}
			super.addInfoMessage("successfully added groupPrivilege");

		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
		}
	}

	public void saveAllGroupPrivileges() {
		try {
			administrationController.saveAllPrivilegeMapping(
					this.privilegeMappingList, group.getRecid());
			super.addInfoMessage("Successfully Saved");
		} catch (Exception e) {
			super.addErrorMessage("Error Exist");
			e.printStackTrace();
		}
	}

	public void loadGroupPrivileges() {
		if (group != null && group.getRecid() != null) {
			try {
				groupPrivilegeList = administrationController
						.getAllGroupPrivileges(group.getRecid());
				for (PrivilegeMapping pMap : this.privilegeMappingList) {
					if (groupPrivilegeList != null
							&& groupPrivilegeList.size() > 0) {
						for (GroupPrivilege gp : groupPrivilegeList) {
							if (pMap.getPrivilegeId().longValue() ==gp.getPrivilageId().longValue()) {
								pMap.setAccessId(gp.getAccessId());
								break;
							} else {
								pMap.setAccessId(Constant.NO_ACCESS);
							}
						}
					} else {
						pMap.setAccessId(Constant.NO_ACCESS);
					}
				}
			} catch (Exception e) {
				super.addErrorMessage("Ann Error Occured");
				e.printStackTrace();
			}

		}

	}

	public void putObject() {

	}

	// ------------------//

	public GroupPrivilege getGroupPrivilege() {
		return groupPrivilege;
	}

	public void setGroupPrivilege(GroupPrivilege groupPrivilege) {
		this.groupPrivilege = groupPrivilege;
	}

	public Groups getGroup() {
		return group;
	}

	public void setGroup(Groups group) {
		this.group = group;
	}

	public List<GroupPrivilege> getGroupPrivilegeList() {
		return groupPrivilegeList;
	}

	public void setGroupPrivilegeList(List<GroupPrivilege> groupPrivilegeList) {
		this.groupPrivilegeList = groupPrivilegeList;
	}

	public List<Access> getAccessList() {
		return accessList;
	}

	public void setAccessList(List<Access> accessList) {
		this.accessList = accessList;
	}

	// public List<Privilege> getPrivilegeList() {
	// return privilegeList;
	// }
	//
	// public void setPrivilegeList(List<Privilege> privilegeList) {
	// this.privilegeList = privilegeList;
	// }

	public List<Groups> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Groups> groupList) {
		this.groupList = groupList;
	}

	public List<PrivilegeMapping> getPrivilegeMappingList() {
		return privilegeMappingList;
	}

	public void setPrivilegeMappingList(
			List<PrivilegeMapping> privilegeMappingList) {
		this.privilegeMappingList = privilegeMappingList;
	}

	public void setPrivilegeMapping(PrivilegeMapping privilegeMapping) {
		this.privilegeMapping = privilegeMapping;
	}

	public PrivilegeMapping getPrivilegeMapping() {
		return privilegeMapping;
	}

}
